<?php
/**
 * @var $paramKey
 * @var array $d
 * @var array $richedits
 * @var $parentFormFieldName 
 * @var $keyValues
 * @var $isList
 * 
 * @var callable $paramsTypeCreate
 * @var $genid
 * @var array $tpls
 * @var int $level
 */

use Model\Page;

$label = $d['label'];
$currentRenderId = $genid();
$formFieldName = $parentFormFieldName ? "{$parentFormFieldName}[$paramKey]" : $paramKey;
?>

<div class="line">
    <?php if($label): ?>
        <div class="label"><?php echo $d['label'] ? $d['label'] : $paramKey ?></div>
    <?php endif ?>
    <div class="value">
        <?php if($isList): ?>
            <i class="remove" 
               onmouseover="mouseover_remove_item(this)"
               onmouseout="mouseout_remove_item(this)"
               onclick="remove_list_item(this)"
               >×</i>
        <?php endif; ?>
        <?php switch($d['type']):
            case Page::PT_TEXT: ?>
            <input type="text" name="<?php echo $formFieldName ?>" value="<?php echo $keyValues ?>" />
            <?php break;case Page::PT_LIST: ?>
            <div class="list-child">
                <?php
                foreach (array_values((array)$keyValues) as $i => $val) {
                    echo $paramsTypeCreate("{$formFieldName}[$i]", $d['children_params'], null, $val, true, $level+1);
                }
                ?>
            </div>
            <button class="button" increment="<?php echo is_array($keyValues) ? count($keyValues) : 0 ?>" type="button" dataid="<?php echo "{$currentRenderId}" ?>" onclick="add_list_item(this)">添加 <?php echo $d['label'] ?></button>
            <?php
            $tmpContent = $paramsTypeCreate("{$formFieldName}[{seq_$currentRenderId}]", $d['children_params'], null, [], true);
            $tpls []= "<script type=\"text/html\" id=\"tpl_{$currentRenderId}\">{$tmpContent}</script>";
            ?>

            <?php break;case Page::PT_SWITCH: ?>
                <?php $_val = $keyValues === null ? $d['default'] : $keyValues ?>
                <label><input type="radio" value="1" name="<?php echo $formFieldName ?>" <?php echo ['', 'checked'][!!$_val] ?>><?php echo $d['on_text']?$d['on_text']:'开启' ?></label>&nbsp;
                <label><input type="radio" value="" name="<?php echo $formFieldName ?>" <?php echo ['', 'checked'][!$_val] ?>><?php echo $d['off_text']?$d['off_text']:'关闭' ?></label>
            <?php break;case Page::PT_OBJECT: ?>
            <?php foreach ($d['nodes'] as $nodekey => $node):  ?>
                <?php echo $paramsTypeCreate("$nodekey", $node, $formFieldName, $keyValues[$nodekey], false, $level+1); ?>
            <?php endforeach; ?>
            <?php 
                break;
                case Page::PT_TEXTAREA:
                case Page::PT_RICHTEXT:
            ?>
            <textarea style="<?php echo $d['style'] ?>" class="richedit richedit-noinit" name="<?php echo $formFieldName ?>"><?php echo $keyValues ?></textarea>
            <?php endswitch; ?>
    </div>
</div>